using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameInfoWindow : MonoBehaviour
{
	public static GameInfoWindow instance;

	public GameObject holder;

	public UILabel title;

	public RaceInfoUI raceInfoUI;

	public TitanfallUI titanfalUI;

	public TDMinfoUI tdmInfoUI;

	public TDMscore scrollsTdmScore;

	public GameResultWindow resultWindow;

	public bool startWindowHided;

	public GameObject hidebutton;

	public GameObject lobbyButton;

	public Color myPlayerBG;

	public Color myPlayerText;

	[SerializeField]
	private GameObject loadAnimation;

	[SerializeField]
	private UIAnimation holderUIAnimation;

	[SerializeField]
	private GameObject timeLabel;

	[SerializeField]
	private GameObject killsLabel;

	[SerializeField]
	private GameObject teamScrolls;

	[SerializeField]
	private PlayersInfoScroll raceScroll;

	[SerializeField]
	private PlayersInfoScroll teamScroll1;

	[SerializeField]
	private PlayersInfoScroll teamScroll2;

	[SerializeField]
	private GameObject restartButton;

	[SerializeField]
	private UISprite bgEnemyTeam;

	[SerializeField]
	private UISprite bgOurTeam;

	[SerializeField]
	private Color zombieTeam;

	[SerializeField]
	private Color enemyTeam;

	[SerializeField]
	private Color ourTeam;

	[SerializeField]
	private GameObject moneyPanel;

	[SerializeField]
	private GameObject shopPanel;

	[SerializeField]
	private UILabel goalLabel;

	private bool refreshLeaderboard = true;

	private void Awake()
	{
		instance = this;
		TurnLoadAnimation(false);
		holder.gameObject.SetActive(false);
		raceScroll.scrollView.disableDragIfFits = true;
		teamScroll1.scrollView.disableDragIfFits = true;
		teamScroll2.scrollView.disableDragIfFits = true;
		ShowRaceInfo(false);
		ShowTitanfallInfo(false);
	}

	public void ShowRaceInfo(bool show)
	{
		if (show)
		{
			raceInfoUI.Reset();
		}
		raceInfoUI.gameObject.SetActive(show);
	}

	public void ShowTDMInfo(bool show)
	{
		tdmInfoUI.gameObject.SetActive(show);
	}

	public void ShowTitanfallInfo(bool show)
	{
		titanfalUI.gameObject.SetActive(show);
	}

	public void TurnLoadAnimation(bool turnOn)
	{
		if (loadAnimation != null)
		{
			loadAnimation.SetActive(turnOn);
		}
	}

	public void HideInterface()
	{
		TurnLoadAnimation(false);
		holder.gameObject.SetActive(false);
		resultWindow.Hide();
	}

	public void UpdateTitle()
	{
		title.text = GameController.instance.CurrentGameMode.GameModeTitle();
	}

	public void ShowStartGameWindow()
	{
		TurnLoadAnimation(false);
		refreshLeaderboard = true;
		if (!startWindowHided)
		{
			MapWindow.instance.gameObject.SetActive(false);
			holder.gameObject.SetActive(true);
			raceScroll.gameObject.SetActive(false);
			teamScrolls.SetActive(false);
			resultWindow.Hide();
			TDMController.instance.SetStartPVP(GameController.instance.CurrentGameMode.IsAutoStart() && GameController.instance.curTypeGame != TypeGame.Zombe);
			hidebutton.SetActive(!GameController.instance.CurrentGameMode.IsAutoStart());
			restartButton.SetActive(false);
			lobbyButton.SetActive(true);
			UpdateTitle();
			UpdateScrollForCurrentGameMode();
			ShowShopAndMoney(false, false);
		}
	}

	public void ShowEndGameWindow()
	{
		if (CarSteerArrow.instance != null)
		{
			CarSteerArrow.instance.gameObject.SetActive(false);
		}
		if ((bool)ChatWindow.instance && ChatWindow.instance.gameObject.GetActive())
		{
			ChatWindow.instance.RefreshEndGame();
			WindowManager.instance.HideWindow(typeof(ChatWindow));
		}
		if (PvPLeaderbord.instance != null)
		{
			PvPLeaderbord.instance.EnableLeaderboard(false);
		}
		resultWindow.HideResult();
		raceScroll.gameObject.SetActive(false);
		teamScrolls.SetActive(false);
		hidebutton.SetActive(false);
		restartButton.SetActive(true);
		lobbyButton.SetActive(true);
		refreshLeaderboard = true;
		BCWDebug.LogError("ShowEndGameWindow");
		title.text = GameController.instance.CurrentGameMode.EndGameModeTitle();
		UpdateScrollForCurrentGameMode();
		if (!instance.holder.activeInHierarchy)
		{
			CoroutineRunner.Instance.StartCoroutine(ActivateHolderWithDelay());
			resultWindow.Show();
		}
		refreshLeaderboard = false;
	}

	public IEnumerator ActivateHolderWithDelay()
	{
		if (GameController.instance.curTypeGame == TypeGame.normal || GameController.instance.curTypeGame == TypeGame.OnlineSandbox || GameController.instance.curTypeGame == TypeGame.Offline)
		{
			yield return new WaitForSeconds(2.5f);
		}
		else if (GameController.instance.curTypeGame == TypeGame.TitanfallV2)
		{
			yield return new WaitForSeconds(1f);
		}
		else
		{
			yield return new WaitForSeconds(4.5f);
		}
		holder.gameObject.SetActive(true);
		TurnLoadAnimation(false);
		holderUIAnimation.AnimAlpha(0f, 1f, 0.5f, false);
	}

	public void InitResultWindow()
	{
		if (resultWindow != null)
		{
			resultWindow.Init();
		}
		resultWindow.HideResult();
	}

	public void Hide()
	{
		GameController.instance.CurrentGameMode.StartRound();
		OnRoundStarting();
	}

	public void RestartGame()
	{
		if (GameController.instance.curTypeGame == TypeGame.race && lobbyButton != null)
		{
			lobbyButton.GetComponent<Collider>().enabled = false;
		}
		if (resultWindow != null)
		{
			resultWindow.Init();
		}
		refreshLeaderboard = true;
		TurnLoadAnimation(true);
		GameController.instance.CurrentGameMode.RestartRound();
		startWindowHided = false;
		CupsManager.SetMaxStarsInCurrentRound();
	}

	public void BackToLobby()
	{
		holder.gameObject.SetActive(false);
		GameController.instance.exitToMenu();
	}

	public void UpdateScrollForCurrentGameMode()
	{
		switch (GameController.instance.CurrentGameMode.TypeOfScrollForGameMode())
		{
		case PlayersScrollType.Deathmatch:
			SetDeathmatchScrolls();
			break;
		case PlayersScrollType.Race:
			SetRaceScroll();
			break;
		case PlayersScrollType.GeneralTeamMatch:
			SetTeamScrolls();
			break;
		case PlayersScrollType.ZombieScroll:
			SetZombieScrolls();
			break;
		}
	}

	public void UpdateDeathmatchScrolls()
	{
		if (raceScroll.gameObject.activeInHierarchy)
		{
			SetDeathmatchScrolls();
		}
	}

	public void UpdateRaceScroll()
	{
		if (!RaceController.instance.gameIsEnded)
		{
			StartCoroutine(UpdateRaceScrollWithDelay());
		}
	}

	public void ShowShopAndMoney(bool? showShop, bool? showMoney)
	{
		shopPanel.SetActive((!showShop.HasValue) ? shopPanel.activeSelf : showShop.Value);
		moneyPanel.SetActive((!showMoney.HasValue) ? moneyPanel.activeSelf : showMoney.Value);
	}

	public void PaintGoalLabel(Color color, string text)
	{
		goalLabel.color = color;
		goalLabel.text = text;
		goalLabel.gameObject.SetActive(true);
	}

	private void OnRoundStarting()
	{
		InitResultWindow();
		UpdateTitle();
		GameController.instance.showWindowGame();
		TurnLoadAnimation(true);
		startWindowHided = true;
		MapWindow.instance.gameObject.SetActive(false);
		ShowMiniMapWithDelay();
		CupsManager.SetMaxStarsInCurrentRound();
	}

	private void SetDeathmatchScrolls()
	{
		raceScroll.gameObject.SetActive(true);
		timeLabel.SetActive(false);
		killsLabel.SetActive(true);
		List<PlayersInfoScroll.PlayerInfoPanelParams> playersList = GameController.instance.CurrentGameMode.GetPlayersList<PlayerBehavior>(null);
		UpdateScrollWithData(raceScroll, playersList);
	}

	private void SetZombieScrolls()
	{
		if (TDMColorScroll.instance != null)
		{
			TDMColorScroll.instance.ChangeColorTeam();
		}
		SetTeamScrolls();
	}

	private void SetTeamScrolls()
	{
		if (refreshLeaderboard)
		{
			scrollsTdmScore.UpdateScores();
			teamScrolls.SetActive(true);
			List<PlayersInfoScroll.PlayerInfoPanelParams> playersList = GameController.instance.CurrentGameMode.GetPlayersList((PlayerBehavior x) => x.localTeam == TDMController.myTeam);
			List<PlayersInfoScroll.PlayerInfoPanelParams> playersList2 = GameController.instance.CurrentGameMode.GetPlayersList((PlayerBehavior x) => x.localTeam == TDMController.enemyTeam);
			UpdateScrollWithData(teamScroll1, playersList);
			UpdateScrollWithData(teamScroll2, playersList2);
		}
	}

	private void SetRaceScroll()
	{
		raceScroll.gameObject.SetActive(true);
		timeLabel.SetActive(true);
		killsLabel.SetActive(false);
		List<PlayersInfoScroll.PlayerInfoPanelParams> playersList = GameController.instance.CurrentGameMode.GetPlayersList<RaceResult>(null);
		UpdateScrollWithData(raceScroll, playersList);
	}

	private void UpdateScrollWithData(PlayersInfoScroll scroll, List<PlayersInfoScroll.PlayerInfoPanelParams> data)
	{
		scroll.ClearScroll();
		foreach (PlayersInfoScroll.PlayerInfoPanelParams datum in data)
		{
			scroll.UpdatePlayerInfo(datum.place, datum.league, datum.name, datum.state1, datum.state2, datum.bgColor, datum.textColor, datum.flagScore, datum.playerId);
		}
		scroll.grid.repositionNow = true;
		RepositionGrid(scroll.grid);
	}

	private IEnumerator UpdateRaceScrollWithDelay()
	{
		yield return new WaitForSeconds(0.5f);
		SetRaceScroll();
	}

	private void RepositionGrid(UIGrid grid)
	{
		CoroutineRunner.Instance.StartCoroutine(RepositionGridWithDelay(grid));
	}

	private IEnumerator RepositionGridWithDelay(UIGrid grid)
	{
		yield return null;
		if (grid != null)
		{
			grid.repositionNow = true;
		}
	}

	private void OnApplicationPause(bool paused)
	{
		if (paused)
		{
			instance.resultWindow.GetawayResult();
		}
	}

	public void ChangeColorTeam()
	{
		if (GameController.instance.curTypeGame == TypeGame.Zombe && TDMController.myTeam != 0)
		{
			if (TDMController.myTeam == PunTeams.Team.blue)
			{
				bgOurTeam.color = ourTeam;
				bgEnemyTeam.color = zombieTeam;
			}
			else
			{
				bgOurTeam.color = zombieTeam;
				bgEnemyTeam.color = ourTeam;
			}
		}
	}

	private void ShowMiniMapWithDelay()
	{
		Invoke("ShowMiniMap", 4f);
	}

	private void ShowMiniMap()
	{
		MapWindow.instance.gameObject.SetActive(true);
	}
}
